package com.samba.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import com.samba.entites.Category;

public class CategoryConverter implements Converter {

	public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String string) {
		
		try{
			int id = Integer.parseInt(string);
			if(id ==0)
			{
				throw new Exception();
			}
			Category c = new Category();
			c.setId(id);
			return c;
		}
		catch(Exception e)
		{
			return null;	
		}
	}
	public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object object) {
		if(object instanceof Category )
		{
			Category o = (Category) object;
			return "" + o.getId();
		}
		else
		{
		 throw new IllegalArgumentException("object :" + object+ "of type" + object.getClass().getName()+ "; excepted type:  com.samba.entites.Category;" )	;
		}
	
	
	}

}
